/*
 * Name: Joon Ho Cho
 * Date: 10/24/2008
 * Course: 15-437
 */
package scheduler.model;


import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;
import org.mybeans.dao.GenericDAO;
import org.mybeans.factory.Transaction;

import scheduler.databean.Schedule;

public class ScheduleDaoBeanFactoryImpl
    extends GenericDAO<Schedule> 
    implements ScheduleDao { 
	
    public ScheduleDaoBeanFactoryImpl(String dbDriver, String dbUrl) {
        this(BeanTable.getSQLInstance(Schedule.class, "schedule", dbDriver, dbUrl));
    }
    
    private ScheduleDaoBeanFactoryImpl(BeanTable<Schedule> table) {
        super(table, "id");
        getTable().setIdleConnectionCleanup(true);
    }
    
    public Schedule[] getSchedule(String userName, int scheduleNum) throws DAOException {
        try {
            return getFactory().match(MatchArg.equals("userName", userName), MatchArg.equals("ScheduleNumber", scheduleNum));
            
        } catch (RollbackException e) {
            throw new DAOException(e);
        }
    }
    

    
    
    public Schedule create() throws DAOException {
        try {
            return getFactory().create();
        } catch (RollbackException e) {
            throw new DAOException(e);
        }
    }
    
    public Schedule[] CheckAdded(String userID, int scheduleNumber, int courseID) throws DAOException {        
        	
        	 try {
                 return getFactory().match(MatchArg.equals("userName", userID),MatchArg.equals("scheduleNumber", scheduleNumber),MatchArg.equals("courseID", courseID));
             } catch (RollbackException e) {
                 throw new DAOException(e);
                 
             }       	
    }
    
    
    
    public void changeSection(int id, String section) throws DAOException {
        try {
        	Transaction.begin();
        	Schedule schedule = lookup(id);
        	schedule.setSectionId(schedule.getCourseId()+section);
			Transaction.commit();
        } catch (RollbackException e) {
            throw new DAOException(e);
        } finally {
        	if (Transaction.isActive()) Transaction.rollback();
        }
    }
    
    
    
    public Schedule create(String userName, int ScheduleNum, int courseId, String sectionName, int year, String semester)
        throws DAOException {
        try {
            Transaction.begin();
            Schedule newSchedule = create();
            newSchedule.setUserName(userName);
            newSchedule.setScheduleNumber(ScheduleNum);
            newSchedule.setCourseId(courseId);
            newSchedule.setSectionId(courseId + sectionName);
            newSchedule.setYear(year);
            newSchedule.setSemester(semester);
            Transaction.commit();
            return newSchedule;
        } catch (RollbackException e) {
            throw new DAOException(e);
        }
    }
    
    public Schedule lookup(int id) throws DAOException {
        try {
            return getFactory().lookup(id);
        } catch (RollbackException e) {
            throw new DAOException(e);
        }
    }
    
  /* public void update(int id, String comment) throws DAOException {
        try {
        	Transaction.begin();
        	Schedule dbBookmark = lookup(id);
        	dbBookmark.setComment(comment);
			Transaction.commit();
        } catch (RollbackException e) {
            throw new DAOException(e);
        } finally {
        	if (Transaction.isActive()) Transaction.rollback();
        }
    }
    */
    public void delete(int id) throws DAOException {
        try {
            getFactory().delete(id);
        } catch (RollbackException e) {
            throw new DAOException(e);
        }
    }

	
}
